package knowlwdge_graph.knowledge_etl.yago_etl;

import com.univocity.parsers.tsv.TsvParser;
import com.univocity.parsers.tsv.TsvParserSettings;
import common.helper.toolkit.FolderToolkit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by julianzliu on 4/18/2017.
 */
public class ExtractorTSVFiles {



    public ExtractorTSVFiles(){

    }

    public List<String> getAllFiles(String folder){
        return FolderToolkit.getAllFiles(folder);
    }

    /*
    public List<RowYagoTriple> getAllTripleInFile(String file){

        //https://www.mkyong.com/java/java-read-a-text-file-line-by-line/


        File f = new File(file);

        List<RowYagoTriple> rows = new ArrayList<>();

        try{
            BufferedReader b = new BufferedReader(new FileReader(f));
            String readLine = "";
            while ((readLine = b.readLine()) != null) {
                //System.out.println(readLine);
                rows.add( parseLineToTripe( readLine) );
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }
    */

    public List<RowYagoTriple> getAllTripleByTSVParser(String file){
        File f = new File(file);
        TsvParserSettings settings = new TsvParserSettings();
        //the file used in the example uses '\n' as the line separator sequence.
        //the line separator sequence is defined here to ensure systems such as MacOS and Windows
        //are able to process this file correctly (MacOS uses '\r'; and Windows uses '\r\n').
        settings.getFormat().setLineSeparator("\n");

        // creates a TSV parser
        TsvParser parser = new TsvParser(settings);

        // parses all rows in one go.
        List<String[]> allRows = parser.parseAll(f);


        List<RowYagoTriple> rows = new ArrayList<>();
        System.out.println("rowsNum = " + allRows.size());
        for(String[] row:allRows){
            //System.out.println(row.toString());
            rows.add( parseRowToTripe( row ) );
        }
        return rows;
    }

    private RowYagoTriple parseRowToTripe(String[] cols){
        //System.out.println("size = " + cols.length);
        //System.out.println(cols[0] + "|" + cols[1] + "|" + cols[2] + "|" + cols[3] + "|" + cols[4]);
        String value = cols[4];
        if(value==null) value = "0";
        String id = cols[0];
        if(id==null) id="";
        else id = id.replace("'","\\'");
        return new RowYagoTriple(
                id,cols[1].replace("'","\\'"),cols[2].replace("'","\\'"),cols[3].replace("'","\\'"),Float.parseFloat(value)
        );
    }

    /*
    private RowYagoTriple parseLineToTripe(String line){
        String[] cols = line.split("\t");
        System.out.println("line = " + line);
        System.out.println("size = " + cols.length);

        return new RowYagoTriple(
            cols[0],cols[1],cols[2],cols[3],Float.parseFloat(cols[4])
        );
    }

*/

    /*********************************************
     * MAIN
     */
    public static void main(String[] args){
        String folder = "/home/gammaliu/Datas/yago/yago3/copy_to_postgres";
        folder = "D:\\GitRepos\\semantic_search_engine\\e11.knowledge-graph\\src\\main\\java\\knowlwdge_graph\\knowledge_etl\\yago_etl\\tsv_files";

        ExtractorTSVFiles extractor = new ExtractorTSVFiles();
        List<String> files = extractor.getAllFiles(folder);

        for(String file:files){
            List<RowYagoTriple> rows = extractor.getAllTripleByTSVParser(file);
        }
    }

}
